# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/07/16 16:48
# @File           : views.py
# @IDE            : PyCharm
# @desc           : 模拟Oauth2登录接口

from fastapi import APIRouter, Depends, Request, Response
from utils.response import SuccessResponse
from application import settings
from core.dependencies import QueryParams


app = APIRouter()


class LoginParams(QueryParams):
    """
    列表分页
    """
    def __init__(
            self,
            username: str,
            password: str
    ):
        super().__init__()
        self.username = username
        self.password = password



@app.get("/sso-server/doLogin3", summary="账号密码登录", description="账号密码登录")
async def login(
        param: LoginParams = Depends(),
):
    if param.username == "admin" and param.password == "admin":
        data = {
            "userid": 111111111,
            "username": "superAdmin",
            "userCode": 111111,
            "userAccount": "superAdmin",
            "employeeId": "00002",
            "employeeCode": "ARJ21事业部",
            "roleList": [
                {
                    "roleId": 11111,
                    "roleName": "用户组件admin超管",
                    "roleCode": "superAdmin",
                    "isDeletedRole": "N"
                }
            ]
        }
        response = SuccessResponse(data=data, code=0, message="success")
        response.set_cookie(settings.COOKIE_KEY, "111111111_74adaff63ad94579942692424a92b8741")
        return response

    else:
        return SuccessResponse(code=-1, message="failed")


@app.post("/sso-server/logout", summary="登出")
async def token_validate(
    response: Response
):
    response.delete_cookie(settings.COOKIE_KEY)
    return SuccessResponse("Cookie deleted")


@app.post("/sso-server/getUserInfo", summary="验证Token")
async def token_validate(
    request: Request,
):
    cookie = request.cookies.get(settings.COOKIE_KEY)
    if cookie == "111111111_74adaff63ad94579942692424a92b8741":
        data = {
            "userid": 111111111,
            "username": "superAdmin",
            "userCode": 111111,
            "userAccount": "superAdmin",
            "employeeId": "00002",
            "employeeCode": "ARJ21事业部",
            "roleList": [
                {
                    "roleId": 11111,
                    "roleName": "用户组件admin超管",
                    "roleCode": "superAdmin",
                    "isDeletedRole": "N"
                }
            ]
        }
        return SuccessResponse(data=data, code=0, message="success")
    else:
        return SuccessResponse(code=-1, message="failed")
